package com.es.disped.wechat;

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import com.alibaba.fastjson.JSONObject;
import com.es.disped.api.http.HttpService;
import com.es.disped.common.wechat.http.config.WeChatConfig;
import com.es.disped.common.wechat.http.request.AccessRequest;
import com.es.disped.common.wechat.http.response.AccessResponse;
import com.es.disped.common.wechat.model.TemplateMsg;

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations={
		"classpath:spring/applicationContext.xml",
		"classpath:spring/springMVC-servlet.xml",
})
public class Message {

    private static final String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?";
    
    @Autowired
    HttpService httpService;
    
    @Test
    public void testSendTemplate() {
    	AccessResponse response=null;
    	AccessRequest request = new AccessRequest(WeChatConfig.getAppId(), WeChatConfig.getAppsecret());
        // 构建请求参数
        Map<String, String> map = new HashMap<String, String>();
        map.put("grant_type", request.getGrant_type());
        map.put("appid", request.getAppid()); 
        map.put("secret", request.getSecret());
        String result = httpService.doGet(ACCESS_TOKEN_URL, map);
        response = JSONObject.parseObject(result, AccessResponse.class);
        System.out.println("response:>>>>>>>>>>>>>>>>>>>>>>>>>>>"+response);
    	
        
//        TemplateMsgResponse templateMsgResponse = null;
        TreeMap<String, TreeMap<String,String>> params = new TreeMap<String,TreeMap<String,String>>();
        //根据具体模板参数组装
        params.put("first", TemplateMsg.item("处方审核未通过，请修改处方", "#000000"));
        params.put("prescriptionCode",TemplateMsg.item("RP2018091020001", "#000000"));
        params.put("doctorName",TemplateMsg.item("彭慰医师", "#000000"));
        params.put("auditStatus",TemplateMsg.item("未通过", "#000000"));
        params.put("prescriptionState",TemplateMsg.item("待修改", "#000000"));
        params.put("auditTime",TemplateMsg.item("2018-09-10 22:22:00", "#000000"));
        params.put("remark",TemplateMsg.item("你开处的处方不合格，请注意修改", "#000000"));
        TemplateMsg templateMsg = new TemplateMsg();
        templateMsg.setTemplate_id("Xg_CUCHKHpPgaKkGoneWxPKJBmldknmlwBknpOZYHGA");
        templateMsg.setTouser("oSKX-0tdbbSeiDJifJrz1o45zHtc");
        templateMsg.setUrl("http://music.163.com/#/song?id=27867140");
        templateMsg.setData(params);
//        ObjectMapper object = new ObjectMapper();
        String data = null;
        String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+response.getAccess_token();
//        data = object.writeValueAsString(wechatTemplateMsg);
        data = JSONObject.toJSONString(templateMsg);
        System.out.println("data:"+data);
        String msgResult = httpService.postJson(url, data);
        System.out.println("输出结果："+msgResult);
    }
}
